package September20;

import java.nio.charset.StandardCharsets;

public class ChineseStringExample {
    public static void main(String[] args) {
        String chineseString = "汉字";

        // 输出每个字符
        System.out.println("字符：");
        for (int i = 0; i < chineseString.length(); i++) {
            char ch = chineseString.charAt(i);
            System.out.println("'" + ch + "' (" + (int)ch + ")");
        }

        // 输出每个字符对应的字节（使用UTF-8编码）
        System.out.println("\nUTF-8 编码下的字节：");
        byte[] bytes = chineseString.getBytes(StandardCharsets.UTF_8);
        for (byte b : bytes) {
            System.out.print(b + " ");
        }
        System.out.println();

        // 输出每个字符对应的字节（使用GBK编码）
        System.out.println("GBK 编码下的字节：");
        bytes = chineseString.getBytes(StandardCharsets.ISO_8859_1); // 需要转换为对应的编码
        for (byte b : bytes) {
            System.out.print(b + " ");
        }
        System.out.println();
    }
}
